

*************************************************
*************************************************
***The Swing Justice
***Peter K. Enns and Patrick Wohlfarth
***The Journal of Politics
*************************************************
*************************************************

************************
**Replication File
**This do file replicates the predicted probabilities and corresponding confidence intervals reported in "The Swing Justice"
**based on the MEDIAN SWING JUSTICE analysis.
**The methods and code are based on Hanmer and Kalkan (AJPS 2013)
************************

pause on

*0) Set the memory to 1000 megabytes and number of variables to 32,767.

set mem 1000m
set maxvar 32767

*1) Get the data set and then run the model.

************************
**NOTE: Must set the working directory
************************
cd ""

**Open Data
use "swingJustice_jopFINpub.dta"

**Standardize cp_reclib 
egen cplibz = std(cp_reclib) if reversal==1 & sw_medmq8==1 & term<2008

**Estimate the median swing justice model
logit libvote sgconslib mood5208  cplibz zsop_phmsm judpower economics civillib if reversal == 1 & term<2008 & sw_medmq8==1, cluster(term) 

*1a) Drop observations that are not in the model so that the quantities of interest are not calculated for those observations.

gen keep = 1 if e(sample)
drop if keep ~=1

*1b) Save the data as a temporary file named temp.

save temp, replace

*2a) Create a mean vector for the coefficients and covariance matrix, draw 1000 sets of coefficients from the multivariate normal using this info.
**Note, following Hanmer and Kalkan (AJPS 2013) that that random number seed is set to 99 (which they chose as it is Wayne Gretzky's number).
**Note that this will temporarily clear the original data set and create a new data set with the simulated coefficients.

  set seed 99
  mat b = e(b)
  mat V = e(V)
  drawnorm b_sgconslib b_mood5208 b_cplibz b_zsop_phmsm b_judpower b_economics b_civillib b_cons, mean(b) cov(V) n(1000) clear


*2b) Merge the original data set (stored as temp) into the data set containing the simulated coefficients.

merge using temp


*3)  For each simulated set of coefficients (this was set to 1000 in step 2a above) create a variable that ///
contains the quantity of interest for each observation for each set of simulated coefficients (i.e. loop over the 1000 sets of simulated coefficients).
**For each quantity of interest, this creates 1000 new variables with values for each observation in the data set.  They are structured as follows.
**Row 1 for the first new variable contains the quantity of interest using values for the first observation and the first set of simulated coefficients.
**Row n for the first new variable contains the quantity of interest using values for the nth observation and the first set of simulated coefficients.
**Row 1 for the second new variable contains the quantity of interest using values for the first observation and the second set of simulated coefficients.
**Row n for the second new variable contains the quantity of interest using values for the nth observation and the second set of simulated coefficients.
**This process continues for each of the 1000 simulated sets of coefficients.

**Then the mean of each of the new variables for a given quantity of interest is stored in a new variable with a name ending in _mean.
**Row 1 for this new variable ending in _mean contains the mean across all of the observations using the first set of simulated coefficients ///
(i.e. the mean of the first new variable described above).
**Row 2 for this new variable ending in _mean contains the mean across all of the observations using the second set of simulated coefficients ///
(i.e. the mean of the second new variable described above).
**This continues through row 1000 which contains the mean across all of the observations using the thousandth set of simulated coefficients ///
(i.e. the mean of the thousandth new variable described above).


**3a) Calculate the predicted probability of success with all variables set to their observed values.
*Start by creating a variable named p_mean that is set to a missing value and is then filled in after each of the 1000 variables are created. 
*Next, calculate the quantity of interest for each observation and each set of simulated coefficients, looping over the 1000 sets of simulated coefficients.
*Finally, fill in the p_mean variable with the mean across all observations from each set of simulated coefficients.

 gen p_mean = .
 forvalues i = 1/1000 {
    gen p_`i' = 1/(1+exp(-1*(b_cons[`i'] + sgconslib*b_sgconslib[`i'] + mood5208*b_mood5208[`i'] + cplibz*b_cplibz[`i'] + zsop_phmsm*b_zsop_phmsm[`i'] + judpower*b_judpower[`i'] + economics*b_economics[`i'] + civillib*b_civillib[`i']))) 
    summarize p_`i', meanonly
    replace p_mean = r(mean) in `i'
}
drop p_1-p_1000

**3b) Calculate the predicted probability of success when the variable of interest is set to a particular value and all covariates are set to their observed values.
*Start by creating a variable named px1_1_mean that is set to a missing value and is then filled in after each of the 1000 variables are created.
*Next, calculate the quantity of interest for each observation and each set of simulated coefficients, looping over the 1000 sets of simulated coefficients.
*Finally, fill in the px1_1_mean variable with the mean across all observations from each set of simulated coefficients.

****************
**Mood
****************

**MOOD (1960)
 gen mood60_mean = .
 forvalues i = 1/1000 {
   gen mood60_`i' = 1/(1+exp(-1*(b_cons[`i'] + sgconslib*b_sgconslib[`i'] + 63.977*b_mood5208[`i'] + cplibz*b_cplibz[`i'] + zsop_phmsm*b_zsop_phmsm[`i'] + judpower*b_judpower[`i'] + economics*b_economics[`i'] + civillib*b_civillib[`i']))) 
   summarize mood60_`i', meanonly
   replace mood60_mean = r(mean) in `i'
}

 gen mood80_mean = .
 forvalues i = 1/1000 {
   gen mood80_`i' = 1/(1+exp(-1*(b_cons[`i'] + sgconslib*b_sgconslib[`i'] + 50.854*b_mood5208[`i'] + cplibz*b_cplibz[`i'] + zsop_phmsm*b_zsop_phmsm[`i'] + judpower*b_judpower[`i'] + economics*b_economics[`i'] + civillib*b_civillib[`i']))) 
   summarize mood80_`i', meanonly
   replace mood80_mean = r(mean) in `i'
}

**3d) Calculate the effect of the variable of interest when moving from the specific values set above (i.e. the first difference).
*Start by creating a variable named effectx1_mean that is set to a missing value and is then filled in after each of the 1000 variables are created.
*Next, calculate the quantity of interest for each observation and each set of simulated coefficients, looping over the 1000 sets of simulated coefficients.
*Finally, fill in the effectx1_mean variable with the mean across all observations from each set of simulated coefficients.

 gen mood60_80_mean = .
 forvalues i = 1/1000 {
   gen mood60_80_`i' = mood60_`i' - mood80_`i'
   summarize mood60_80_`i', meanonly
   replace mood60_80_mean = r(mean) in `i' 
}
drop mood60_1 - mood60_1000
drop mood80_1 - mood80_1000



****************
**Justice Ideology
****************

**IDEOLOGY (20th percentile; Burger)
*sum cplibz if justiceName == "WEBurger" & e(sample)

 gen ideol20_mean = .
 forvalues i = 1/1000 {
   gen ideol20_`i' = 1/(1+exp(-1*(b_cons[`i'] + sgconslib*b_sgconslib[`i'] + mood5208*b_mood5208[`i'] + -.9105749*b_cplibz[`i'] + zsop_phmsm*b_zsop_phmsm[`i'] + judpower*b_judpower[`i'] + economics*b_economics[`i'] + civillib*b_civillib[`i']))) 
   summarize ideol20_`i', meanonly
   replace ideol20_mean = r(mean) in `i'
}

**IDEOLOGY (80th percentile; WJBrennan)
*sum cplibz if justiceName == "WJBrennan" & e(sample)

 gen ideol80_mean = .
 forvalues i = 1/1000 {
   gen ideol80_`i' = 1/(1+exp(-1*(b_cons[`i'] + sgconslib*b_sgconslib[`i'] + mood5208*b_mood5208[`i'] + .8518343*b_cplibz[`i'] + zsop_phmsm*b_zsop_phmsm[`i'] + judpower*b_judpower[`i'] + economics*b_economics[`i'] + civillib*b_civillib[`i']))) 
   summarize ideol80_`i', meanonly
   replace ideol80_mean = r(mean) in `i'
}

 gen ideol20_80_mean = .
 forvalues i = 1/1000 {
   gen ideol20_80_`i' = ideol20_`i' - ideol80_`i'
   summarize ideol20_80_`i', meanonly
   replace ideol20_80_mean = r(mean) in `i' 
}

drop ideol20_1 - ideol20_1000
drop ideol80_1 - ideol80_1000
******************************
******************************

****************
**Solicitor General Brief
****************

**SG (conservative)

 gen consSG_mean = .
 forvalues i = 1/1000 {
   gen consSG_`i' = 1/(1+exp(-1*(b_cons[`i'] + -1*b_sgconslib[`i'] + mood5208*b_mood5208[`i'] + cplibz*b_cplibz[`i'] + zsop_phmsm*b_zsop_phmsm[`i'] + judpower*b_judpower[`i'] + economics*b_economics[`i'] + civillib*b_civillib[`i']))) 
   summarize consSG_`i', meanonly
   replace consSG_mean = r(mean) in `i'
}

**SG (liberal)

 gen libSG_mean = .
 forvalues i = 1/1000 {
   gen libSG_`i' = 1/(1+exp(-1*(b_cons[`i'] + 1*b_sgconslib[`i'] + mood5208*b_mood5208[`i'] + cplibz*b_cplibz[`i'] + zsop_phmsm*b_zsop_phmsm[`i'] + judpower*b_judpower[`i'] + economics*b_economics[`i'] + civillib*b_civillib[`i']))) 
   summarize libSG_`i', meanonly
   replace libSG_mean = r(mean) in `i'
}


 gen SGcons_lib_mean = .
 forvalues i = 1/1000 {
   gen SGcons_lib_`i' = consSG_`i' - libSG_`i'
   summarize SGcons_lib_`i', meanonly
   replace SGcons_lib_mean = r(mean) in `i' 
}


sum mood60_80_mean
centile mood60_80_mean, centile(2.5, 97.5)

sum ideol20_80_mean
centile ideol20_80_mean, centile(2.5, 97.5)

sum SGcons_lib_mean
centile SGcons_lib_mean, centile(2.5, 97.5)



drop cplibz 



